<template>
  <el-button type="primary" @click="setPlatform" style="margin-bottom: 20px;">添加新的平台</el-button>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" border>
      <el-table-column label="平台名称" width="180">
        <template #default="scope">{{ scope.row.platformName }}</template>
      </el-table-column>
      <el-table-column label="添加时间" width="180">
        <template #default="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="delectPlatform(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!-- <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4"
     :page-sizes="[10, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div> -->
  <!-- 添加平台 -->
  <div>
    <el-dialog v-model="dialogVisible" width="500">
      <span style="display: flex; align-items: center">平台名称：<el-input v-model="platformName" style="width: 240px"
          :rows="3" type="textarea" placeholder="多个以回车换行" /></span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false, platformName = ''">取消</el-button>
          <el-button type="primary" @click="submitPlatform">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { procurementPlatformCreate, procurementPlatformlist, procurementPlatformRemove } from '@/api/index'
import { ElMessage } from 'element-plus'
// do not use same name with ref
// const form = ref({
//  pageSize: 10,
//   pageNum: 1
// })
const tableData = ref([])
onBeforeMount(() => {
  getList()
})
const getList = () => {
  procurementPlatformlist().then(res => {
    const { data: { data } } = res
    // total.value = data.total
    tableData.value = data
  })
}
// 分页
// const currentPage4 = ref(1)
// const pageSize4 = ref(10)
// const total = ref(0)
// const handleSizeChange = (val) => {
//   console.log(`${val} items per page`)
//   form.value.pageSize = val
//   getList()
// }
// const handleCurrentChange = (val) => {
//   console.log(`current page: ${val}`)
//   form.value.pageNum = val
//   getList()
// }
// 平台
const dialogVisible = ref(false)
const platformName = ref('')
const setPlatform = () => {
  dialogVisible.value = true
  console.log('submit!')
}
const submitPlatform = () => {
  // 使用正则表达式去除所有类型的空格
  // const noSpaceString = platformName.value.replace(/\s/g, '')
  // 输出结果
  const platformArr = platformName.value.split('\n')
  const filteredArr = platformArr.filter(item => item.trim() !== '')
  procurementPlatformCreate({ platformNames: filteredArr }).then(res => {
    dialogVisible.value = false
    ElMessage.success('添加成功')
  })
}
// 删除
const delectPlatform = (row) => {
  procurementPlatformRemove(row.id).then(res => {
    ElMessage.success('删除成功')
    getList()
  })
}
</script>

<style lang='less' scoped></style>
